package at.fhj.itm09.snake.activities;

import java.io.File;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import at.fhj.itm09.snake.R;
import at.fhj.itm09.snake.logic.SnakeLogic;
import at.fhj.itm09.snake.views.SnakeView;


/**
 * @author Susanne Brandstätter - Susanne.Brandstaetter.ITM09@fh-joanneum.at
 * @author Katharina Biela - Katharina.Biela.ITM09@fh-joanneum.at
 *
 * This activity is called by clicking on the play button in the main activity. 
 * 
 * It loads all settings for playing, the buttons to navigate the snake
 * and the view where the game will be played. 
 */

public class PlaySnakeActivity extends Activity {

	private ImageButton btnDown;
	private ImageButton btnLeft;
	private ImageButton btnRight;
	private ImageButton btnUp;
	
	//View where the game will be played
    private SnakeView mSnakeView;
    private static String ICICLE_KEY = "snake-view";
	private int headColor;
    private Boolean rbGreen;
    private Boolean rbRed;
    private Boolean rbYellow;
    private ImageView iVBackground;
    private Boolean myBackground;
    private Boolean blackBackground;
    private Boolean loadBackground;
    
    private SnakeLogic snakeLogic;
    
    
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.snake);
		
		btnDown = (ImageButton) findViewById(R.id.btnDown);
		btnLeft = (ImageButton) findViewById(R.id.btnLeft);
		btnUp = (ImageButton) findViewById(R.id.btnUp);
		btnRight = (ImageButton) findViewById(R.id.btnRight);
		
        mSnakeView = (SnakeView) findViewById(R.id.snake);
        mSnakeView.setTextView((TextView) findViewById(R.id.text));
        iVBackground = (ImageView) findViewById(R.id.ivBackground);
        

        setBackgroundForPlaying();
        
        snakeLogic = new SnakeLogic(mSnakeView, btnDown, btnUp, btnLeft, btnRight);

        /*
         * Sets up a new game
         */
    	setTheHeadColorForTheSnake();
    	mSnakeView.setHeadColorForSnake(getHeadColor());
    	
    	mSnakeView.setMode(SnakeView.READY);
	}
	

    @Override
    protected void onPause() {
        super.onPause();
        
        // Pause the game
        mSnakeView.setMode(SnakeView.PAUSE);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        //Stores the game state
        outState.putBundle(ICICLE_KEY, mSnakeView.saveState());
    }
	
    /**
     * Sets the background for the game
     */
    public void setBackgroundForPlaying(){
        SharedPreferences settingPrefs = getSharedPreferences("mySharedSettings", 0);
        blackBackground = settingPrefs.getBoolean("rbBlack", false);
        myBackground = settingPrefs.getBoolean("rbMyPhoto", false);
        loadBackground = settingPrefs.getBoolean("rbLoadPhoto", false);
         
        //Try to load the taken and loaded picture
        File mySnakePhoto = new File("/sdcard/","mySnakePhoto.jpg");
        File loadSnakePhoto = new File("/sdcard/", "myLoadedSnakeimage.png");
       
        
        if(blackBackground == true){
        	iVBackground.setImageDrawable(null);
        } 
        if(myBackground == true){
        	//Only put the taken photo in the background, if it exists
            if (mySnakePhoto.exists()) {
		    	String imageInSD = "/sdcard/mySnakePhoto.jpg";
		        Drawable d = Drawable.createFromPath(imageInSD);
		        iVBackground.setImageDrawable(d);
            }
        }    	
        if(loadBackground == true){
        	//Only put the loaded photo in the background, if it exists
        	if(loadSnakePhoto.exists()){
        		String loadImageInSD = "/sdcard/myLoadedSnakeimage.png";
		        Drawable d = Drawable.createFromPath(loadImageInSD);
		        iVBackground.setImageDrawable(d);
        	}
        }
    }
    
    
    /**
     * Sets the color for the snake's head. 
     */
    public void setTheHeadColorForTheSnake(){
		SharedPreferences settingPrefs = getSharedPreferences("mySharedSettings", 0);
		rbGreen = settingPrefs.getBoolean("rbGreen", false);
		rbRed = settingPrefs.getBoolean("rbRed", false);
		rbYellow = settingPrefs.getBoolean("rbYellow", false);
	
		if(rbRed == true){
			this.setHeadColor(1);
		}
		if(rbYellow == true){
			this.setHeadColor(2);
		}
		if(rbGreen == true){
			this.setHeadColor(3);
		}
    }
   
	public void setHeadColor(int headColor) {
		this.headColor = headColor;
	}

	public int getHeadColor() {
		return headColor;
	}
}
